import { isEditableArea, toCells, isSameType } from ".";
import CellType from "../../../excel/celltypes";

export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        context.suspendPaint();
        try {
            const { sheet, sourceSheet, cellRange, oldValue, oldText, callback, sourceRange, value } = options;
            const targetCells = toCells({ ...cellRange, sheet });
            if (isUndo) {
                targetCells.forEach((cell, index) => {
                    cell.value(oldValue[index]);
                    cell.text(oldText[index]);
                });
            } else {
                options.oldValue = targetCells.map(cell => cell.value());
                options.oldText = targetCells.map(cell => cell.text());
                if (sourceSheet && sourceRange) {
                    const sourceCells = toCells({ ...sourceRange, sheet: sourceSheet });
                    let getSourceCell;
                    if (sourceCells.length === 1) {
                        getSourceCell = () => {
                            return sourceCells[0]
                        }
                    } else {
                        getSourceCell = (index) => {
                            return sourceCells[index];
                        }
                    }
                    targetCells.forEach((cell, index) => {
                        if (isEditableArea(cell)) {
                            const sourceCell = getSourceCell(index);
                            if (sourceCell && isSameType(cell, sourceCell) && cell.cellType() && cell.cellType().viewAttr != '1') {
                                cell.text(sourceCell.text());
                                cell.cellType().doUpdateValue(cell, sourceCell.value());
                            }
                        }
                    });
                } else {
                    if (value) {
                        const valueTr = value.split('\r\n');
                        const values = [];
                        valueTr.forEach(tr => {
                            tr.split('	').forEach(td => {
                                values.push(td);
                            })
                        })
                        let getValue;
                        if (values.length === 1) {
                            getValue = () => {
                                return values[0]
                            }
                        } else {
                            getValue = (index) => {
                                return values[index];
                            }
                        }
                        const WeaInputCellType = CellType('WeaInputCellType');
                        targetCells.forEach((cell, index) => {
                            if (isEditableArea(cell) && cell.cellType() && cell.cellType().viewAttr != '1') {
                                const value = getValue(index);
                                if (cell.cellType() instanceof WeaInputCellType) {
                                    cell.text(value);
                                    cell.cellType().doUpdateValue(cell, value);
                                }
                            }
                        });
                    }
                }
            }
            callback();
        } catch (e) {
            if (!!(window.console && window.console.log)) {
                window.console.log(e);
            }
        }
        context.resumePaint();
    }
};